<?php 
namespace App\Http\Controllers\TongGaoApi;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;


use Excel;

//use Illuminate\Http\Response;
class ExcelActionController extends Controller
{	
	const ROUTE_PREFIX = '/api/*';
	protected $request;
	protected $curTime;
	protected $debug;
	/*
	参考文档
	1、https://www.jianshu.com/p/84ef90964304
	2、https://laravel-excel.maatwebsite.nl/docs/3.0/getting-started/basics
	*/
	/*excle 导入*/
	public function importExcel(){
		$file = Input::file('excel');   //获取上传文件

		$clientName = $file -> getClientOriginalName();   //上传文件名
		$entension = $file -> getClientOriginalExtension(); //上传文件的后缀.
		$realPath = $file -> getRealPath();   //临时文件绝对路径，如果不上传服务器的话可直接读取该文件
		//$path = $file -> move('D:/123');  //如果要上传的话请在这里调试
		//$filePath = 'storage/exports/'.iconv('UTF-8', 'GBK', '学生成绩').'.xls'; //此处需注意文件名的编码问题
		Excel::load($realPath, function($reader) {
			
			$reader = $reader->getSheet(0);
			
			$results = $reader->toArray();
			print_r($results);
	        //$data = $reader->all();
	        //dd($data);
	    });

	}
	
	
	/*excle 导出 demo*/
	public function exportExcelDemo(){
		echo 'hello';
        $cellData = [
            //['学号','姓名','成绩'],
            ['10001','AAAAA','99'],
            ['10002','BBBBB','92'],
            ['10003','CCCCC','95'],
            ['10004','DDDDD','89'],
            ['10005','EEEEE','96'],
        ];
        Excel::create('tt',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
				ob_end_clean();
            });
        })->export('xls');
    }

	public static function myImportExcel($realPath,$sheetIndex=0){
		$cellData = [];
		echo "before load " . date("h:i:sa")."</br>";
		Excel::load($realPath, function($reader) use (&$cellData, $sheetIndex) {
			echo "after load " . date("h:i:sa")."</br>";
			$reader = $reader->getSheet(0);
			echo "after get sheet " . date("h:i:sa")."</br>";
			$cellData = $reader->toArray();
			echo "after toArray " . date("h:i:sa")."</br>";
			
	        //$data = $reader->all();
	        //dd($data);
	    });
		echo count($cellData,2);
		return $cellData;
	}
	
	public static function exportExcel($outputFileName,$cellData){
		if (empty($cellData))
			return;
		//var_dump($cellData);
        Excel::create($outputFileName,function($excel) use ($cellData){
            $excel->sheet('术语', function($sheet) use ($cellData){
                //$sheet->rows($cellData); ob_end_clean();//workable, but we will use another method in order to set style
				
				//填充每个单元格的内容
				$rowCount = count($cellData);
				$colCount = count($cellData[0]);
				echo "rowCount=$rowCount colCount=$colCount</br>";
				for($rowIndex=0;$rowIndex<$rowCount;$rowIndex++){
					for($colIndex=0;$colIndex<$colCount;$colIndex++){
						$cellRange = sprintf("%s%u",chr(65+$colIndex),$rowIndex+1);
						$cellValue = $cellData[$rowIndex][$colIndex];
						echo "cellRange=$cellRange</br>";
						$sheet->cell($cellRange,function($cell) use ($cellValue)
						{
							$cell->setValue($cellValue);
						});	
						$sheet->getStyle($cellRange)->getAlignment()->setWrapText(true);
						$sheet->getStyle($cellRange)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_TOP);
					}
				}
				//$sheet->getColumnDimension('B')->setWidth(88);
				//$sheet->getColumnDimension('C')->setWidth(300);
				ob_end_clean();
            });
        })->export('xlsx');
		//})->store('xls')->export('xls');
		
		//return new Response($file, 200, [
        //    'Content-Type' => 'application/vnd.ms-excel; charset=UTF-8',
        //    'Content-Disposition' => 'attachment; filename="' . $file->filename . '.' . $file->ext . '"',
        //    'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', // Date in the past
        //    'Last-Modified' => Carbon::now()->format('D, d M Y H:i:s'),
        //    'Cache-Control' => 'cache, must-revalidate',
        //    'Pragma' => 'public',
        //]);		
    }
	
	public static function getExcelSheetIndexBySheetName($reader,$needleSheetName){
		$sheetCount = $reader->getSheetCount();       // 获取工作表的个数  
		$sheetNames = $reader->getSheetNames();		
		foreach($sheetNames as $sheetIndex=>$sheetName){
			if (trim($sheetName)==$needleSheetName)
				return $sheetIndex;
		}
		return false;
	}

}
?>